package org.ysling.litemall.admin.annotation.context;
/**
 *  Copyright (c) [ysling] [927069313@qq.com]
 *  [litemall-plus] is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *              http://license.coscl.org.cn/MulanPSL2
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 *  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.ysling.litemall.admin.annotation.entity.Permission;
import org.ysling.litemall.admin.annotation.handler.PermissionHandler;
import org.ysling.litemall.db.vomain.PermVo;

import java.util.*;

/**
 * 权限服务类
 */
@Component
public class PermissionsContext {

    @Autowired
    private ApplicationContext context;

    private Set<String> systemPermissionsSet = null;
    private List<PermVo> systemPermissionList = null;
    private HashMap<String, String> systemPermissionsMap = null;
    private final String basicPackage = "org.ysling.litemall.admin";

    /**
     * 将系统权限转换成API
     * @param permissions 权限列表
     * @return 转换后Api列表
     */
    public Collection<String> toApi(Set<String> permissions) {
        if (systemPermissionsMap == null) {
            systemPermissionsMap = new HashMap<>();
            List<Permission> systemPermissions = PermissionHandler.listPermission(context, basicPackage);
            for (Permission permission : systemPermissions) {
                String perm = permission.getRequiresPermissions().value()[0];
                String api = permission.getApi();
                systemPermissionsMap.put(perm, api);
            }
        }
        Collection<String> apis = new HashSet<>();
        if (permissions.contains("*")){
            apis.addAll(systemPermissionsMap.values());
            return apis;
        }
        for (String perm : permissions) {
            String api = systemPermissionsMap.get(perm);
            apis.add(api);
        }
        return apis;
    }

    /**
     * 获取权限列表
     */
    public Set<String> getSystemPermissionsSet() {
        if (systemPermissionsSet == null){
            List<Permission> permissions = PermissionHandler.listPermission(context, basicPackage);
            systemPermissionList = PermissionHandler.listPermVo(permissions);
            systemPermissionsSet = PermissionHandler.listPermissionString(permissions);
        }
        return systemPermissionsSet;
    }

    public List<PermVo> getSystemPermissionList() {
        if (systemPermissionList == null){
            List<Permission> permissions = PermissionHandler.listPermission(context, basicPackage);
            systemPermissionList = PermissionHandler.listPermVo(permissions);
            systemPermissionsSet = PermissionHandler.listPermissionString(permissions);
        }
        return systemPermissionList;
    }

}
